package com.tools.scanner.ui.qrcode.scanner

import android.os.Bundle
import android.text.TextUtils
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.google.mlkit.vision.barcode.common.Barcode
import com.tools.scanner.R
import com.tools.scanner.bean.CalendarData
import com.tools.scanner.databinding.FragmentCalendarScannerBinding
import com.tools.scanner.utils.saveCalendar
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Locale
import java.util.TimeZone

class CalendarFragment : BaseScannerFragment() {

    private lateinit var mBinding: FragmentCalendarScannerBinding

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        mBinding = FragmentCalendarScannerBinding.inflate(inflater, container, false)
        return mBinding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        val calendarEvent = getData(Barcode.CalendarEvent::class.java) ?: return

        val startTime = calendarEvent.start
        if (startTime == null) {
            mBinding.llStartTime.visibility = View.GONE
        } else {
            mBinding.tvStartTime.text = formatCalendarDateTime(startTime)
        }

        val endTime = calendarEvent.end
        if (endTime == null) {
            mBinding.llEndTime.visibility = View.GONE
        } else {
            mBinding.tvEndTime.text = formatCalendarDateTime(endTime)
        }

        mBinding.tvEvent.text = calendarEvent.summary

        val location = calendarEvent.location
        if (TextUtils.isEmpty(location)) {
            mBinding.llLocation.visibility = View.GONE
        } else {
            mBinding.tvLocation.text = location
        }

        val organizer = calendarEvent.organizer
        if (TextUtils.isEmpty(organizer)) {
            mBinding.llOrganizer.visibility = View.GONE
        } else {
            mBinding.tvOrganizer.text = organizer
        }

        val description = calendarEvent.description
        if (TextUtils.isEmpty(description)) {
            mBinding.llDescription.visibility = View.GONE
        } else {
            mBinding.tvDescription.text = description
        }

        mBinding.tvSave.setOnClickListener {
            calendarEvent.saveCalendar()
        }
    }

    private fun formatCalendarDateTime(dateTime: Barcode.CalendarDateTime): String {
        val formatter = SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault()).apply {
            timeZone = TimeZone.getDefault()
        }

        val sourceTimeZone = if (dateTime.isUtc) {
            TimeZone.getTimeZone("UTC")
        } else {
            TimeZone.getDefault()
        }
        val calendar = Calendar.getInstance(sourceTimeZone).apply {
            set(Calendar.YEAR, dateTime.year)
            set(Calendar.MONTH, dateTime.month - 1)
            set(Calendar.DAY_OF_MONTH, dateTime.day)
            set(Calendar.HOUR_OF_DAY, dateTime.hours)
            set(Calendar.MINUTE, dateTime.minutes)
            set(Calendar.SECOND, dateTime.seconds)
        }

        return formatter.format(calendar.time)
    }

}